package translated;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import edu.pku.sei.modeler.model.core.*;
import edu.pku.sei.modeler.jet.util.*;
import edu.pku.sei.modeler.model.notation.core.*;
import edu.pku.sei.modeler.model.notation.layout.*;
import edu.pku.sei.modeler.model.notation.shape.*;
import edu.pku.sei.modeler.model.notation.shape.extradata.*;
import edu.pku.sei.gefcommon.common.*;

public class FigureGen
{
  protected static String nl;
  public static synchronized FigureGen create(String lineSeparator)
  {
    nl = lineSeparator;
    FigureGen result = new FigureGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "/**" + NL + " * Copyright (c) Software Institute of PKU" + NL + " * All rights reserved. " + NL + " * This program and the accompanying materials" + NL + " * are made available under the terms of the Eclipse Public License v1.0" + NL + " * which accompanies this distribution, and is available at" + NL + " * http://www.eclipse.org/legal/epl-v10.html" + NL + " *" + NL + " */" + NL + "package ";
  protected final String TEXT_2 = ";" + NL + "" + NL + "import java.util.ArrayList;" + NL + "import java.util.List;" + NL + "" + NL + "import org.eclipse.draw2d.Graphics;" + NL + "import org.eclipse.draw2d.ColorConstants;" + NL + "import org.eclipse.draw2d.CompoundBorder;" + NL + "import org.eclipse.draw2d.Figure;" + NL + "import org.eclipse.draw2d.IFigure;" + NL + "import org.eclipse.draw2d.Label;" + NL + "import org.eclipse.draw2d.LineBorder;" + NL + "import org.eclipse.draw2d.MarginBorder;" + NL + "import org.eclipse.draw2d.ToolbarLayout;" + NL + "import org.eclipse.draw2d.geometry.Rectangle;" + NL + "import org.eclipse.draw2d.XYLayout;" + NL + "import org.eclipse.swt.graphics.Color;" + NL + "" + NL + "import edu.pku.sei.modeler.figures.CommonFigure;" + NL + "import edu.pku.notation.runtime.draw2d.*;" + NL + "import edu.pku.notation.runtime.layout.*;" + NL + "" + NL + "/**" + NL + " * @generated" + NL + " *" + NL + " */" + NL + "public class ";
  protected final String TEXT_3 = "Figure extends CommonFigure {" + NL + "\tpublic static final org.eclipse.swt.graphics.Color ";
  protected final String TEXT_4 = "_COLOR = new Color(null,175,178,239);" + NL + "\tpublic IFigure root = null;" + NL + "\t";
  protected final String TEXT_5 = NL + "\tprivate TextFigure text";
  protected final String TEXT_6 = " = null;" + NL + "\t";
  protected final String TEXT_7 = NL + "\tpublic ";
  protected final String TEXT_8 = "Figure(){" + NL + "\t\t";
  protected final String TEXT_9 = NL + "\t\tToolbarLayout layout = new ToolbarLayout();" + NL + "\t\tsetLayoutManager(layout);" + NL + "\t\tsetBackgroundColor(";
  protected final String TEXT_10 = "_COLOR);" + NL + "\t\tsetBorder(new CompoundBorder(new LineBorder(ColorConstants.buttonDarker,1), new MarginBorder(1)));" + NL + "\t\t";
  protected final String TEXT_11 = NL + "\t\tIFigure f = null;" + NL + "\t\tIFigure parent = null;" + NL + "\t\tXYLayout layout = new XYLayout();" + NL + "\t\tsetLayoutManager(layout);" + NL + "\t\tList parentList = new ArrayList<IFigure>();" + NL + "\t\tparentList.add(this);" + NL + "\t\t";
  protected final String TEXT_12 = NL + "\t\tf = new RectangleFigure();" + NL + "\t\t";
  protected final String TEXT_13 = NL + "\t\tf = new TriangleFigure(";
  protected final String TEXT_14 = ");" + NL + "\t\t";
  protected final String TEXT_15 = NL + "\t\tf = new DiamondFigure();" + NL + "\t\t";
  protected final String TEXT_16 = NL + "\t\tf = new RoundedRectangleFigure();" + NL + "\t\t";
  protected final String TEXT_17 = NL + "\t\tf = new RoundedRectangleFigure();" + NL + "\t\t((RoundedRectangleFigure)f).setCornerWidth(";
  protected final String TEXT_18 = ");" + NL + "\t\t((RoundedRectangleFigure)f).setCornerHeight(";
  protected final String TEXT_19 = ");" + NL + "\t\t";
  protected final String TEXT_20 = NL + "\t\tf = null;" + NL + "\t\t";
  protected final String TEXT_21 = NL + "\t\tf = null;\t" + NL + "\t\t";
  protected final String TEXT_22 = NL + "\t\tf = new EllipseFigure();" + NL + "\t\t";
  protected final String TEXT_23 = NL + "\t\tf = new CircleFigure();" + NL + "\t\t";
  protected final String TEXT_24 = NL + "\t\tf = null;" + NL + "\t\t";
  protected final String TEXT_25 = "\t" + NL + "\t\tf = PolylineFigure.createVerticalLineFigure();" + NL + "\t\t";
  protected final String TEXT_26 = NL + "\t\tf = PolylineFigure.createHorizontalLineFigure();" + NL + "\t\t";
  protected final String TEXT_27 = NL + "\t\tf = null" + NL + "\t\t";
  protected final String TEXT_28 = NL + "\t\tf = new TextFigure();" + NL + "\t\ttext";
  protected final String TEXT_29 = " = (TextFigure)f;" + NL + "\t\t((TextFigure)f).setText(\"";
  protected final String TEXT_30 = "\");" + NL + "\t\t((TextFigure)f).setAlignment(1);" + NL + "\t\t";
  protected final String TEXT_31 = "parent = (IFigure)parentList.get(";
  protected final String TEXT_32 = ");" + NL + "\t\tparent.add(f);" + NL + "\t\tf.setBackgroundColor(new org.eclipse.swt.graphics.Color(null, ";
  protected final String TEXT_33 = ", ";
  protected final String TEXT_34 = ", ";
  protected final String TEXT_35 = "));" + NL + "\t\tf.setForegroundColor(new org.eclipse.swt.graphics.Color(null, ";
  protected final String TEXT_36 = ", ";
  protected final String TEXT_37 = ", ";
  protected final String TEXT_38 = "));" + NL + "\t\tf.setBounds(new ";
  protected final String TEXT_39 = ");" + NL + "\t\t";
  protected final String TEXT_40 = "f.setLayoutManager(new VectorGraphLayout());" + NL + "\t\t";
  protected final String TEXT_41 = NL + "\t\tf.setLayoutManager(new FlowLayout());" + NL + "\t\t";
  protected final String TEXT_42 = NL + "\t\tf.setLayoutManager(new FlowLayout(false));" + NL + "\t\t";
  protected final String TEXT_43 = NL + "\t\tf.setLayoutManager(new XYLayout());" + NL + "\t\t";
  protected final String TEXT_44 = NL + "\t\t((VectorGraphLayout)parent.getLayoutManager()).setConstraint(parent, f);" + NL + "\t\t";
  protected final String TEXT_45 = NL + "\t\t((FlowLayout)parent.getLayoutManager()).setConstraint(parent,f,";
  protected final String TEXT_46 = ",";
  protected final String TEXT_47 = ",";
  protected final String TEXT_48 = ");" + NL + "\t\t";
  protected final String TEXT_49 = NL + "\t\t((FlowLayout)parent.getLayoutManager()).setConstraint(parent,f,";
  protected final String TEXT_50 = ",";
  protected final String TEXT_51 = ",";
  protected final String TEXT_52 = ");" + NL + "\t\t";
  protected final String TEXT_53 = "parentList.add(f);" + NL + "\t\t";
  protected final String TEXT_54 = NL + "\t\tif(parentList.size()>1)" + NL + "\t\t{" + NL + "\t\t\troot = (IFigure)parentList.get(1);" + NL + "\t\t\troot.setBounds(new Rectangle(0,0,root.getBounds().width, root.getBounds().height));" + NL + "\t\t}" + NL + "\t\t";
  protected final String TEXT_55 = NL + "\t\t" + NL + "\t}" + NL + "\t";
  protected final String TEXT_56 = NL + "\tpublic TextFigure getText";
  protected final String TEXT_57 = "() {" + NL + "\t\treturn text";
  protected final String TEXT_58 = ";" + NL + "\t}" + NL + "\t";
  protected final String TEXT_59 = NL + "\tpublic void setBounds(Rectangle rect) {" + NL + "\t\tsuper.setBounds(rect);" + NL + "\t\tif (root != null)" + NL + "\t\t\troot.setSize(rect.width, rect.height);" + NL + "\t}" + NL + "\tprotected void paintShadow(Graphics g) {" + NL + "\t\t// TODO: draw the figure's own shadow here" + NL + "\t}" + NL + "}";
  protected final String TEXT_60 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     MetaClass cls = (MetaClass) argument; 
    stringBuffer.append(TEXT_1);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.figureBase);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_3);
    stringBuffer.append(cls.getName().toUpperCase());
    stringBuffer.append(TEXT_4);
    
		Notation notation = null;
		CoreMetaModel cmm = (CoreMetaModel)cls.getModel();
		List nlist = cmm.getNotations();
		for (int i = 0; i < nlist.size();i++) {
			Notation n = (Notation)nlist.get(i);
			if (n.getModelID() == cls.getElementID()) {
				notation = n;
				break;
			}//end if
		}//end for
		
		// get all TextObject so that we can generate getters for them
		// and then we can call the getters to get them, and change their text
		// used for Property-binding
		List<TextObject> textObjs = new ArrayList<TextObject>();
		if(notation != null) {
			textObjs = notation.getAllTextObj();
		}
		for(int i=0; i<textObjs.size(); i++) {
			TextObject tobj = textObjs.get(i);
	
    stringBuffer.append(TEXT_5);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_6);
    
		}
	
    stringBuffer.append(TEXT_7);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_8);
    
		if (notation == null) {
		
    stringBuffer.append(TEXT_9);
    stringBuffer.append(cls.getName().toUpperCase());
    stringBuffer.append(TEXT_10);
     
		}// end if
		else {
		
    stringBuffer.append(TEXT_11);
    
			List notationsList = new ArrayList<List>();
			notationsList.add(notation.getNotations());
			for(int j = 0; j < notationsList.size(); j++)
			{
				List notations = (List)notationsList.get(j);
				
				for (int i = 0; i < notations.size(); i++) {
					Shape s = (Shape)notations.get(i);
					if (s instanceof Polygon) {
						Polygon p = (Polygon)s;	
						switch(p.getType()) { 
							case Polygon.RECTANGLE:
		
    stringBuffer.append(TEXT_12);
    
							break;
							case Polygon.TRIANGLE:
		
    stringBuffer.append(TEXT_13);
    stringBuffer.append(((TriangleData)p.getExtraData()).getDirection());
    stringBuffer.append(TEXT_14);
    
							break;
							case Polygon.DIAMOND:
		
    stringBuffer.append(TEXT_15);
    
							break;
							case Polygon.CORNERRECTANGLE:
		
    stringBuffer.append(TEXT_16);
    
							break;
							case Polygon.ROUNDEDRECTANGLE:
		
    stringBuffer.append(TEXT_17);
    stringBuffer.append(((RoundedRectangleData)p.getExtraData()).getWidth());
    stringBuffer.append(TEXT_18);
    stringBuffer.append(((RoundedRectangleData)p.getExtraData()).getHeight());
    stringBuffer.append(TEXT_19);
    
							break;
							case Polygon.COSTUMPOLYGON:
		
    stringBuffer.append(TEXT_20);
    
							break;
							default:
		
    stringBuffer.append(TEXT_21);
    
						}//end switch
					}//end if (s instanceof Polygon)
					else if (s instanceof Ellipse) {
						Ellipse e = (Ellipse)s;
						switch(e.getType()) {
							case Ellipse.ELLIPSE:
		
    stringBuffer.append(TEXT_22);
    
							break;
							case Ellipse.CIRCLE:		
		
    stringBuffer.append(TEXT_23);
    
							break;
							default:
		
    stringBuffer.append(TEXT_24);
    
						}//end switch
					}//end else if (s instanceof Ellipse)
					else if (s instanceof Polyline) {
						Polyline p = (Polyline)s;
						switch(p.getType()) {
							case Polyline.VERTICAL:	
		
    stringBuffer.append(TEXT_25);
    
							break;
						 	case Polyline.HORIZONTAL:
		
    stringBuffer.append(TEXT_26);
    
							break;
							default:
		
    stringBuffer.append(TEXT_27);
    
						}//end switch
					}//end else if s instanceof Polyline
					else if (s instanceof TextObject) {
						TextObject t = (TextObject)s;
		
    stringBuffer.append(TEXT_28);
    stringBuffer.append(t.getElementID());
    stringBuffer.append(TEXT_29);
    stringBuffer.append(t.getText());
    stringBuffer.append(TEXT_30);
    
					}//end else if s instanceof TextObject 
		
    stringBuffer.append(TEXT_31);
    stringBuffer.append(j);
    stringBuffer.append(TEXT_32);
    stringBuffer.append(s.getFillColor().red);
    stringBuffer.append(TEXT_33);
    stringBuffer.append(s.getFillColor().green);
    stringBuffer.append(TEXT_34);
    stringBuffer.append(s.getFillColor().blue);
    stringBuffer.append(TEXT_35);
    stringBuffer.append(s.getLineColor().red);
    stringBuffer.append(TEXT_36);
    stringBuffer.append(s.getLineColor().green);
    stringBuffer.append(TEXT_37);
    stringBuffer.append(s.getLineColor().blue);
    stringBuffer.append(TEXT_38);
    stringBuffer.append(s.getBound());
    stringBuffer.append(TEXT_39);
    
					if (s instanceof Shape) {
						switch(((Shape) s).getLayoutStrategy()) //set layout
						{
							case LayoutNode.TYPE_VECTORGRAPH:
		
    stringBuffer.append(TEXT_40);
    
								break;
							case LayoutNode.TYPE_VERTICAL_FLOW:
		
    stringBuffer.append(TEXT_41);
    
								break;
							case LayoutNode.TYPE_HORIZONTAL_FLOW:
		
    stringBuffer.append(TEXT_42);
    

								break;
							default:
		
    stringBuffer.append(TEXT_43);
    
						}//end switch
					}//end if
					if (s.getParent() != null && s.getParent() instanceof Shape){//set layout constraint
					//modified by fengchao 2007-11-25
					Layout parent_layout = ((Shape) s.getParent()).getLayout();
					//end modified	
						switch(parent_layout.getLayoutStrategy()) 
						{
							case LayoutNode.TYPE_VECTORGRAPH:
		
    stringBuffer.append(TEXT_44);
    
							break;
							case LayoutNode.TYPE_VERTICAL_FLOW:
							VerticalFlowLayoutConstraint vflc = (VerticalFlowLayoutConstraint)parent_layout.getConstraint(s);
		
    stringBuffer.append(TEXT_45);
    stringBuffer.append(vflc.widthHint);
    stringBuffer.append(TEXT_46);
    stringBuffer.append(vflc.heightHint);
    stringBuffer.append(TEXT_47);
    stringBuffer.append(vflc.align);
    stringBuffer.append(TEXT_48);
    
							break;
							//modified by fengchao 2007-11-25
							case LayoutNode.TYPE_HORIZONTAL_FLOW:
							HorizontalFlowLayoutConstraint hflc = (HorizontalFlowLayoutConstraint)parent_layout.getConstraint(s);
		
    stringBuffer.append(TEXT_49);
    stringBuffer.append(hflc.widthHint);
    stringBuffer.append(TEXT_50);
    stringBuffer.append(hflc.heightHint);
    stringBuffer.append(TEXT_51);
    stringBuffer.append(hflc.align);
    stringBuffer.append(TEXT_52);
    
							//end modified
								
							default:
						}//end switch
					}//end if
		
    stringBuffer.append(TEXT_53);
    
					notationsList.add(s.getSlaves());
				}//end for
			}//end for
		
    stringBuffer.append(TEXT_54);
    
		}//end else
		
    stringBuffer.append(TEXT_55);
    
		// generate getters for the TextFigures
		for(int i=0; i<textObjs.size(); i++) {
			TextObject tobj = textObjs.get(i);
	
    stringBuffer.append(TEXT_56);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_57);
    stringBuffer.append(tobj.getElementID());
    stringBuffer.append(TEXT_58);
    
		}
	
    stringBuffer.append(TEXT_59);
    stringBuffer.append(TEXT_60);
    return stringBuffer.toString();
  }
}
